package cn.com.eado.crm.v.adapter

import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.BindingAdapter
import cn.com.eado.crm.app.MyApp
import cn.com.eado.crm.app.utils.DateUtils
import cn.com.eado.crm.m.db.Company
import cn.com.eado.crm.m.db.Institute

object BindingAdapters {

    @JvmStatic
    @BindingAdapter("visibleGone")
    fun showHide(view: View, show: Boolean) {
        view.visibility = if (show) View.VISIBLE else View.GONE
    }

    @JvmStatic
    @BindingAdapter("visibleInVisible")
    fun showHide1(view: View, show: Boolean) {
        view.visibility = if (show) View.VISIBLE else View.INVISIBLE
    }

    @JvmStatic
    @BindingAdapter("topIcon")
    fun textViewIcon(view: TextView, id: Int) {
        val drawable = MyApp.instance.resources.getDrawable(id)
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        view.setCompoundDrawables(null, drawable, null, null)
    }

    @JvmStatic
    @BindingAdapter("resId")
    fun textViewIcon(view: ImageView, id: Int) {
        view.setImageResource(id)
    }

    //未跟踪天数
    @JvmStatic
    @BindingAdapter("un_followup_days")
    fun unFollowUpDays(view: TextView, row: Company.Row?) {
        if (row?.CreateDate != null){
            val date = row.CreateDate.split(" ")[0].split("/")
            val year = date[0].toInt()
            val month = date[1].toInt()
            val day = date[2].toInt()
            val date2 = row.LastTrackData.split(" ")[0].split("/")
            val year2 = date2[0].toInt()
            val month2 = date2[1].toInt()-1
            val day2 = date2[2].toInt()
            val days = DateUtils.getIntervalDays(year, month, day, year2, month2, day2)
            view.text = "$days"
        } else{
            view.text = "无"
        }
    }

    //未跟踪天数
    @JvmStatic
    @BindingAdapter("un_followup_days2")
    fun unFollowUpDays2(view: TextView, row: Institute.Row?) {
        if (row?.CreateDate != null){
            val date = row.CreateDate.split(" ")[0].split("/")
            val year = date[0].toInt()
            val month = date[1].toInt()
            val day = date[2].toInt()
            val date2 = row.LastTrackDate.split(" ")[0].split("/")
            val year2 = date2[0].toInt()
            val month2 = date2[1].toInt()-1
            val day2 = date2[2].toInt()
            val days = DateUtils.getIntervalDays(year, month, day, year2, month2, day2)
            view.text = "$days"
        } else{
            view.text = "无"
        }
    }


//    @JvmStatic
//    @BindingAdapter("imgUrl")
//    fun showImageView(view: ImageView, url: String) {
////        LogUtil.e("tag", "ImageView w = ${view.width} h = ${view.height}")
//        val w: Int = (view.context.resources.displayMetrics.density * 80).toInt()
//        Glide
//            .with(view.context)
//            .load(url)
//            .apply(RequestOptions().override(w, w).fitCenter().centerCrop())
//            .placeholder(R.drawable.ic_photo_black_24dp)
//            .error(R.drawable.ic_photo_error)
//            .into(view)
//    }

}